---
sidebar_position: 1
sidebar_class_name: hidden
---

# Refine

The refine documents chain constructs a response by looping over the input documents and iteratively updating its answer. For each document, it passes all non-document inputs, the current document, and the latest intermediate answer to an LLM chain to get a new answer.

Since the Refine chain only passes a single document to the LLM at a time, it is well-suited for tasks that require analyzing more documents than can fit in the model's context.
The obvious tradeoff is that this chain will make far more LLM calls than, for example, the Stuff documents chain.
There are also certain tasks which are difficult to accomplish iteratively. For example, the Refine chain can perform poorly when documents frequently cross-reference one another or when a task requires detailed information from many documents.

![refine_diagram](/img/refine.jpg)

Here's how it looks in practice:

import RefineExample from "@examples/chains/qa_refine.ts";
import CodeBlock from "@theme/CodeBlock";

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/openai
```

<CodeBlock language="typescript">{RefineExample}</CodeBlock>

## Prompt customization

You may want to tweak the behavior of a step by changing the prompt. Here's an example of how to do that:

import CustomRefineExample from "@examples/chains/qa_refine_custom_prompt.ts";

<CodeBlock language="typescript">{CustomRefineExample}</CodeBlock>
